<?php

/**
 * QQ登录   http://connect.opensns.qq.com/申请appid, appkey, 并注册callback地址
 * 
 * Flan@2012-11-20
*/
class openqqAction extends openapiAction {
	/**
	 * QQ授权api接口.按需调用
	 */
	private $m_scope = '';
	
	private $m_aError = array();
	
	
	function __construct(){
        parent::__construct();
		$this->m_nOpenType = 2;
		$this->m_nOpenTypeName = $this->m_aOpenType[$this->m_nOpenType - 1];
		
		$this->m_appid = 100332814;
		$this->m_appkey = '09e167a4746fd7363da7c4c6a32e9430';
		//$this->m_callback = 'http://member.che1an.com/api/_myqq/oauth/qq_callback.php';
		$this->m_callback = 'http://member.che1an.com/open/openqq/callback';
		$this->m_scope = 'get_user_info,add_share,list_album,add_album,upload_pic,add_topic,add_one_blog,add_weibo';
        $this->debug = true;
	}
	
    function test(){
        $this->loginFlow();
    }
	function loginto(){
		
// 		cookie::set('open['.$this->m_nOpenTypeName.']', '', -3600*24*5);
		
// 		$this->setCookie(array('token' => 1234, 'aa' => 2345));
		
// 		$this->setCookie(array('token' => 1234));
// 		$this->setCookie(array('aa' => 2345));
		
		
		
		// 取得随机验证码, 保存至cookie
		$sCheckState = $this->creatCheckState();
		$this->setCookie(array('state' => $sCheckState));
		
		$login_url = 'https://graph.qq.com/oauth2.0/authorize?';
		$login_url .= 'response_type=code&client_id='.$this->m_appid;
		$login_url .= '&redirect_uri='.urlencode($this->m_callback);
		$login_url .= '&scope='.$this->m_scope;
		$login_url .= '&state='.$sCheckState;
		header("Location:$login_url");
	}
	
	function callback(){
		$sToken = $this->getToken();
		$sOpenId = $this->getOpenId($sToken);
		
		// echo '$sToken:'.$sToken;
		// echo '<br />';
		// echo '$sOpenId:'.$sOpenId;
		// echo '<br />';
		
		$this->m_sToken = $sToken;
		$this->m_sOpenId = $sOpenId;
		
		$this->setCookie(array('token' => $sToken));
		
		//print_r($this->m_aError);
		
 		$aUserInfo = $this->getUserInfo();
		
 		//print_r($aUserInfo);

        $this->m_sNickname = $aUserInfo['nickname'];
		
 		$this->loginFlow();
		
		
		
	}
	
	/**
	 * 生成验证码
	 * 
	 * @access private 
	 * @return String 
	 */
	private function creatCheckState(){
		return md5(uniqid(rand(), TRUE));
	}
	
	
	/**
	 * 取得access_token
	 *
	 * @access public
	 * @return int openid
	 */
	private function getToken(){
		
		$aCookie = $this->getCookie();
		
		// 验证
		if($_REQUEST['state'] != $aCookie['state']) {
			$this->m_aError[] = array(
				'msg' => 'The state does not match. You may be a victim of CSRF.',
			);
			return '';
		}
		
		$token_url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&"
		. "client_id=" . $this->m_appid. "&redirect_uri=" . urlencode($this->m_callback)
		. "&client_secret=" . $this->m_appkey. "&code=" . $_REQUEST["code"];

		$response = file_get_contents($token_url);
		if (strpos($response, "callback") !== false){
			$lpos = strpos($response, "(");
			$rpos = strrpos($response, ")");
			$response  = substr($response, $lpos + 1, $rpos - $lpos -1);
			$msg = json_decode($response);
			if (isset($msg->error)){
				$this->m_aError[] = array(
					'error' => $msg->error,
					'msg' => $msg->error_description,
				);
				
				return '';
			}
		}

		$params = array();
		parse_str($response, $params);

		return $params["access_token"];
	}
	
	/**
	 * 取得openid
	 * 
	 * @access public 
	 * @return int openid
	 */
	private function getOpenId($sToken){
		$graph_url = "https://graph.qq.com/oauth2.0/me?access_token=".$sToken;
	
		$str  = file_get_contents($graph_url);
		if (strpos($str, "callback") !== false){
			$lpos = strpos($str, "(");
			$rpos = strrpos($str, ")");
			$str  = substr($str, $lpos + 1, $rpos - $lpos -1);
		}
	
		$user = json_decode($str);
		if (isset($user->error)){
			$this->m_aError[] = array(
				'error' => $msg->error,
				'msg' => $msg->error_description,
			);
			return '';
		}
		return $user->openid;
	}
	
	/**
	 * 取得用户基础信息
	 * 
	 * @access public 
	 * @return return_type
	 */
	private function getUserInfo(){
		$get_user_info = "https://graph.qq.com/user/get_user_info?"
		. "access_token=" . $this->m_sToken
		. "&oauth_consumer_key=" . $this->m_appid
		. "&openid=" . $this->m_sOpenId
		. "&format=json";
		
		$info = file_get_contents($get_user_info);
		$arr = json_decode($info, true);
		
		return $arr;
	}
	
}
